<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" inline size="small">
            <el-form-item>
              <el-input v-model="queryData.title" clearable placeholder="重量段名称" />
            </el-form-item>

            <el-form-item>
              <el-select v-model="queryData.state" clearable filterable placeholder="状态" @change="list">
                <el-option v-for="item in stateOptions" :key="item.state" :label="item.name" :value="item.state" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="list">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;margin-bottom: 12px;display: flex;">
          <el-button type="primary" @click="add()">新增</el-button>
        </div>
        <!-- 表格区域 -->
        <el-table
          :columns="table_header"
          :data="table_body"
          border
          :max-height="tableHeight"
        >
          <el-table-column prop="title" label="重量段方案名称" min-width="100" align="center" />
          <el-table-column label="状态" min-width="100" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 0" style="color: red">未启用</span>
              <span v-else style="color: #4298ef">已启用</span>
            </template>
          </el-table-column>
          <el-table-column prop="createdBy" label="创建人" min-width="100" align="center" />
          <el-table-column prop="createdAt" label="创建时间" min-width="100" align="center" />
          <el-table-column prop="editor" label="操作人" min-width="100" align="center" />
          <el-table-column prop="editedAt" label="最后更新时间" min-width="100" align="center" />
          <el-table-column label="操作" min-width="100" align="center">
            <template slot-scope="scope">
              <template v-if="scope.row.state == 0">
                <el-button type="text" @click="edit(scope.row.id)">编辑</el-button>
                <el-button type="text" @click="enable(scope.row.id)">启用</el-button>
              </template>
              <el-button type="text" @click="edit(scope.row.id)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <New ref="newWeight" @query="list" />
  </div>
</template>

<script>
import request from '@/utils/request'
import New from './weight_new.vue'
export default {
  components: {
    New
  },
  data() {
    return {
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      queryData: {
        state: '',
        title: '',
        pageSize: 50,
        currentPage: 1
      },
      table_header: [
        {
          title: '重量段方案名称',
          minWidth: 100,
          align: 'center',
          key: 'title'
        },
        {
          title: '状态',
          minWidth: 100,
          align: 'center',
          slot: 'state'
        },
        {
          title: '创建人',
          minWidth: 100,
          align: 'center',
          key: 'createdBy'
        },
        {
          title: '创建时间',
          minWidth: 100,
          align: 'center',
          key: 'createdAt'
        },
        {
          title: '操作人',
          minWidth: 100,
          align: 'center',
          key: 'editor'
        },
        {
          title: '最后更新时间',
          minWidth: 100,
          align: 'center',
          key: 'editedAt'
        },
        {
          title: '操作',
          minWidth: 100,
          align: 'center',
          slot: 'action'
        }
      ],
      table_body: [],
      // 状态选项
      stateOptions: [
        {
          state: 0,
          name: '未启用'
        },
        {
          state: 1,
          name: '已启用'
        }
      ]
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 300
    },
    pageSize() {
      return this.pageSizeOpts[0]
    }
  },
  created() {
    this.list() // 获取表格数据
  },
  methods: {
    add() {
      this.$refs.newWeight.show()
    },
    list() {
      request({
        url: `/weight/query`,
        method: 'post',
        data: this.queryData
      }).then(res => {
        if (res.success) {
          this.pageTotal = res.data.total
          this.table_body = res.data.records
        }
      })
    },
    enable(id) {
      const _this = this
      this.$confirm('重量段方案启用后不可再编辑,确认启用吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        request({
              url: `/weight/enable?id=` + id,
              method: 'get'
            }).then(res => {
              if (res.success) {
                _this.list()
              }
            })
      })
    },
    edit(id) {
      const newWeight = this.$refs.newWeight
      request({
        url: `/weight/detail?id=` + id,
        method: 'get'
      }).then(res => {
        if (res.success) {
          newWeight.edit(res.data)
        }
      })
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }
  }
}
</script>
